package response;

import protocol.CtfActionException;
import utils.Conversions;

/**
 * Represents the server response to a DISCONNECT message type (6).
 * 		Result
 * 			 0	Error on disconnect
 * 			 1	Successful 
 */
public class DisconnectBody {
	public static final int LENGTH = 2;
	
	/* Field set */
	int result;
	
	public int getResult() { return result; }
	
	/**
	 * Parses the received byte array. (Client usage)
	 * @param response
	 * @throws CtfActionException
	 */
	public DisconnectBody( byte[] response ) throws CtfActionException {
		result = Conversions.bytesToInt(response);
		if ( result == -1 )
			throw new CtfActionException("6:disconnect");
	}
	
	/**
	 * Set result field. (Server usage)
	 * @param result
	 */
	public DisconnectBody( int result ) {
		this.result = result;
	}
	
	/**
	 * Converts the DisconnectBody to a byte array in network byte order.
	 * (Server usage)
	 * @return byte array
	 */
	public byte[] toNetworkByteArray() {
		return Conversions.intToBytes(result);
	}
}
